<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<shiro:hasPermission name="QUERY_VIEW_COL_MOD">
<a class="btn btn-primary" id="${uuid}_saveColForViewBt">保存修改</a>
</shiro:hasPermission>

<shiro:hasPermission name="QUERY_VIEW_COL_ADD">
<a class="btn btn-info" id="${uuid}_selectColForViewBt">选择字段</a>
</shiro:hasPermission>

<div  id="${uuid}_viewColumnsBox" class="am-margin-top-sm">
	
</div>

<!-- 显示字段模板 -->
<textarea id='viewColumnDef_tpl' style='display:none;'>
	<form class="viewColDefItem">
	<table class="table table-bordered table-striped table-hover">
	<tr>
		<td>
			<input type="hidden" name="id" value="<!:=id !>">
			<input type="hidden" name="renderScript" value="<!=renderScript !>">
			<input type="hidden" name="queryDefId" value="<!:=queryDefId !>">
			<input type="hidden" name="name" value="<!:=name !>">
			<input type="hidden" name="columnName" value="<!:=columnName !>">
			<input type="hidden" name="tableName" value="<!:=tableName !>">
			<!:=name !>
		</td>
		<td>
			<a style="width: 80px;display:inline-block">显示名</a><input type="text" value="<!:=viewName!>" name="viewName">
			<br/>
			<a style="width: 80px;display:inline-block">列宽</a><input type="text" value="<!:=width!>" name="width">
		</td>
		<td>
			<input type="checkbox" value="1" name="canView">是否显示
			&nbsp;
			<input type="checkbox" value="1" name="sortAble">可以排序 
			<br/>
			<input type="checkbox" value="1" name="isDefaultSort">默认排序
			&nbsp;
			<select name="defaultSortAsc" style="display: none;">
				<option value="ASC">顺序 ASC</option>
				<option value="DESC">倒序 DESC</option>
			</select>
		</td>
		<td>
			<a class="btn btn-link btn-sm _j_upBt">
				<span class="glyphicon glyphicon-chevron-up"></span>
			</a>
			<a class="btn btn-link btn-sm _j_downBt">
				<span class="glyphicon glyphicon glyphicon-chevron-down"></span>
			</a>
			<shiro:hasPermission name="QUERY_VIEW_COL_DEL">
			<a class="btn btn-link btn-sm _j_removeBt">
				<span class="glyphicon glyphicon glyphicon-trash"></span>
			</a>
			</shiro:hasPermission>
		</td>
	</tr>
	<tr>
		<td>
			<select name="renderType">
				<option value="0" selected="selected">原文输出</option>
				<option value="1">时间格式</option>
				<option value="2">脚本</option>
				<option value="3">字典</option>
			</select>
		</td>
		<td colspan="3">
			<div class="renderTypeOpt renderTypeOpt_3" style="display: none;">
				<input type="text" name="renderType_3" value="字典值">
			</div>
			<div class="renderTypeOpt renderTypeOpt_1" style="display: none;">
				<input type="text" name="renderType_1" value="yyyy-MM-dd HH:mm:ss">
			</div>
			<div class="renderTypeOpt renderTypeOpt_2" style="display: none;">
				<p class="am-form-help">可用参数grid,data, row, index,需要return值。</p>
				<textarea  name="renderType_2" style="width: 100%;height: 80px"></textarea>
			</div>
		</td>
	</tr>
	</table>
	</form>
</textarea>	
<script type="text/javascript">
$(function(){
	var uuid = '${uuid}';
	var queryDefId='${param.queryDefId}';
	var viewColumnsBox=$("#" + uuid + "_viewColumnsBox");
	var viewColumnDef_tpl= baidu.template("viewColumnDef_tpl");
	
	var _saveColForViewBt=$("#" + uuid + "_saveColForViewBt").click(function(){
		webUtils.alert("正在保存...");
		if(queryDefId){
			var datas=[];
			viewColumnsBox.find(".viewColDefItem").each(function(i,n){
				var data = webUtils.formToObj($(n));
				if(data.renderType!=0){
					data.renderScript=data["renderType_"+data.renderType];
				}
				datas.push(data);
			});
			$.post(ctx+"/queryDefAction/saveViewColDefs.do",{datas:Base64.encode(JSON.stringify(datas))},function(res){
				if(!res.success){
					webUtils.alert(res.msg);
				}else{
					webUtils.alert("保存成功!");
					loadViewColumnDefs();
				}
			},"json");
		}
	});
	
	var _selectColForViewBt=$("#" + uuid + "_selectColForViewBt").click(function(){
		if(!queryDefId){
			webUtils.alert("请选择左手边具体的查询定义");
			return false;
		}
		var sub = top.webUtils.topicSubscribe("cols.select.success", function (args) {
			columnsDialog.close();
			
			$.each(args,function(i,n){
				var d={
						name:n.label,
						viewName:n.comment?n.comment:n.label,
						width:'auto',
						canView:1,
						sortAble:1,
						renderType:0,
						queryDefId:queryDefId,
						columnName:n.name,
						tableName:n.tableName
				}
				if(viewColumnsBox.find("input[name=name][value="+d.name+"]").length<1){
					var $viewcolumnDef=renderViewColumnDef($(viewColumnDef_tpl(d)),d);
					viewColumnsBox.append($viewcolumnDef);
				}
			});
        });
		var columnsDialog=selectColumn({
			onClose:function(){
				sub.remove();
			}
		});
	});
	
	function loadViewColumnDefs(){
		$.getJSON(ctx+"/queryDefAction/loadViewColDefs.do",{id:queryDefId},function(res){
			viewColumnsBox.html("");
			$.each(res,function(i,n){
				if(viewColumnsBox.find("input[name=name][value="+n.name+"]").length<1){
					var $viewcolumnDef=renderViewColumnDef( $( viewColumnDef_tpl(n) ),n );
					viewColumnsBox.append($viewcolumnDef);
				}
			}) 
		});
	}
	
	function renderViewColumnDef($viewcolumnDef,data){
		$viewcolumnDef.find("._j_removeBt").click(function(){
			var id=$viewcolumnDef.find("input[name=id]").val();
			if(id){
				$.post(ctx+"/queryDefAction/removeViewColDef.do",{id:id},function(res){
					$viewcolumnDef.remove();
					webUtils.alert("删除成功!");
				},"json");
			}else{
				$viewcolumnDef.remove();
				webUtils.alert("删除成功!");
			}
		});
		$viewcolumnDef.find("._j_downBt").click(function(){
			var next=$viewcolumnDef.next();
			if(next.length>0){
				$viewcolumnDef.insertAfter(next);
			}
		});
		$viewcolumnDef.find("._j_upBt").click(function(){
			var prev=$viewcolumnDef.prev();
			if(prev.length>0){
				$viewcolumnDef.insertBefore(prev);
			}
		});
		 
		$viewcolumnDef.find("select[name=renderType]").change(function(){
			$viewcolumnDef.find(".renderTypeOpt").hide();
			$viewcolumnDef.find(".renderTypeOpt_"+$(this).val()).show();
		});
		
		$viewcolumnDef.find("input[name=isDefaultSort]").click(function(){
			if($(this).is("input:checked")){
				$viewcolumnDef.find("select[name=defaultSortAsc]").show();
			}else{
				$viewcolumnDef.find("select[name=defaultSortAsc]").hide();
			}
		});
		
		
		if(data){
			if(data.renderType!=0){
				data["renderType_"+data.renderType]=data.renderScript;
			}
			$viewcolumnDef.find(".renderTypeOpt").hide();
			$viewcolumnDef.find(".renderTypeOpt_"+data.renderType).show();
			
			if(data.isDefaultSort==1){
				$viewcolumnDef.find("select[name=defaultSortAsc]").show();
			}else{
				$viewcolumnDef.find("select[name=defaultSortAsc]").hide();
			}
			
			webUtils.fullForm({
				form:$viewcolumnDef,
				data:data
			});
			
		}
		
		return $viewcolumnDef;
	}
	
	
	loadViewColumnDefs();
});
</script>